var status = getUrlParam('status');
// var dic = new Array();
// dic["0"] = "待付款";
// dic["1"] = "待发货";
// dic["2"] = "待收货";
// dic["11"] = "待提货";
// dic["3"] = "待评价";

var dic = {0: "待付款", 1: "待发货", 2: "待收货", 7:"已完成",10: "已完成", 11: "待提货", 3: "待评价"};

function init() {


    var $cart = $('.menu-item');
    //console.log(cart.length);
    $cart.each(function () {
        var $this = $(this);
        //console.log(this);
        var cid = $this.attr("value");
        if (cid == status) {
            $this.addClass('on')
        }
        console.log(cid);
    });


    $.ajax({

        type: "post",
        url: ajaxUrl + "/mobile/order/myOrder",
        async: true,
        data: {'memberId': memberId, 'status': status, "page": 1, "rows": 100},
        success: function (data) {
            var info = data.result;
            console.log(info);
            var str = '';

            for (var i = 0; i < info.length; i++) {
                str += '<div class="my-order-item">' +
                    '        <a href="#">' +
                    '            <div class="title-wrapper bottom-border">' +
                    '                <div class="title">订单编号：' + info[i].orderNo + '</div>' +
                    '                <div class="status">' + dic[info[i].status] + '</div>' +
                    '            </div>'

                var items = info[i].items;
                console.log(items);
                var itemStr = ""
                for (var j = 0; j < items.length; j++) {
                    itemStr += '            <div class="order-item bottom-border">' +
                        '                <img src="' + items[j].productImage + '" alt="" class="img">' +
                        '                <div class="text">' +
                        '                    <div class="name">' + items[j].productName + '</div>' +
                        '                    <div class="specific">' + items[j].normal + '</div>' +
                        '                </div>' +
                        '                <div class="price-num">' +
                        '                    <div class="price"><i>￥</i>' + items[j].price + '</div>' +
                        '                    <div class="num">X ' + items[j].num + '</div>' +
                        '                </div>' +
                        '            </div>'
                }
                str += itemStr;
                str += '            <div class="num-cost bottom-border">' +
                    '                <div class="num">共计商品：<i>' + info[i].num + '件</i></div>' +
                    '                <div class="cost">应付款：<i>￥' + info[i].totalFee + '</i><em>（运费:￥' + info[i].expressFee + '）</em></div>' +
                    '            </div>' +
                    '        </a>' +
                    '        <div class="btn-wrapper">' +
                    statusBtn(info[i].status, info[i]) +
                    //    '            <div class="btn">取消订单</div>' +
                    //   '            <div class="btn on zhifu-hook">立即支付</div>' +
                    '        </div>' +
                    '        ' +
                    '    </div>';
            }
            $('#orderList').html(str);
        }
    });

}

function getStatus(sta) {
    $.ajax({

        type: "post",
        url: ajaxUrl + "/mobile/order/myOrder",
        async: true,
        data: {'memberId': memberId, 'status': sta, "page": 1, "rows": 100},
        success: function (data) {
            var info = data.result;
            console.log(info);
            var str = '';

            for (var i = 0; i < info.length; i++) {
                str += '<div class="my-order-item">' +
                    '        <a href="#">' +
                    '            <div class="title-wrapper bottom-border">' +
                    '                <div class="title">订单编号：' + info[i].orderNo + '</div>' +
                    '                <div class="status">' + dic[info[i].status] + '</div>' +
                    '            </div>'

                var items = info[i].items;
                console.log(items);
                var itemStr = ""
                for (var j = 0; j < items.length; j++) {
                    itemStr += '            <div class="order-item bottom-border">' +
                        '                <img src="' + items[j].productImage + '" alt="" class="img">' +
                        '                <div class="text">' +
                        '                    <div class="name">' + items[j].productName + '</div>' +
                        '                    <div class="specific">' + items[j].normal + '</div>' +
                        '                </div>' +
                        '                <div class="price-num">' +
                        '                    <div class="price"><i>￥</i>' + items[j].price + '</div>' +
                        '                    <div class="num">X ' + items[j].num + '</div>' +
                        '                </div>' +
                        '            </div>'
                }
                str += itemStr;
                str += '            <div class="num-cost bottom-border">' +
                    '                <div class="num">共计商品：<i>' + info[i].num + '件</i></div>' +
                    '                <div class="cost">应付款：<i>￥' + info[i].totalFee + '</i><em>（运费:￥' + info[i].expressFee + '）</em></div>' +
                    '            </div>' +
                    '        </a>' +
                    '        <div class="btn-wrapper">' +
                    statusBtn(info[i].status, info[i]) +
                    //    '            <div class="btn">取消订单</div>' +
                    //   '            <div class="btn on zhifu-hook">立即支付</div>' +
                    '        </div>' +
                    '        ' +
                    '    </div>';
            }
            $('#orderList').html(str);
        }
    });
}

function confirmOrder(orderId, fee){
   $.ajax({
        type: "post",
        url: ajaxUrl + "/mobile/order/confirmGet",
        async: true,
        data: {'orderId': orderId, 'memberId': memberId},
        success: function (data) {
            if(data.code==0){
                alertMsg("确认成功")
            }else{
                alertMsg(data.message)
            }
            // history.go(0) ;
        }
    });
}
//立即支付
function payNow(orderId, fee) {
    window.location.href = '/payment.html?orderId=' + orderId + "&totalFee=" + fee;
}

function cancel(memberId, orderId) {
    $.ajax({
        type: "post",
        url: ajaxUrl + "/mobile/order/cancelOrder",
        async: true,
        data: {'orderId': orderId, 'memberId': memberId},
        success: function (data) {
            if(data.code==0){
                alertMsg("取消成功")
            }else{
                alertMsg(data.message)
            }
             history.go(0) ;
        }
    });
}

//根据类型判断下面的按钮信息
function statusBtn(status, order) {
    var str = "";


    if (status == 0) {
        str = ' <div onclick="cancel(' + order.memberId + ',' + order.orderId + ')" id="cancel" class="btn">取消订单</div>' +
            ' <div onclick="payNow(' + order.orderId + ',' + order.totalFee + ')" id="" class="btn on zhifu-hook">立即支付</div>'
    }
    if (status == 1) {
        str = '<div id="refund" class="btn on">申请退款</div>';
    }
    if (status == 2) {
        str = '<div id="expression" class="btn wuliu-hook">查看物流</div>' +
            ' <div id="refund" class="btn">申请退货</div>' +
            ' <div onclick="confirmOrder('+ order.orderId + ',' + order.orderId +')" id="confirm" class="btn on">确认收货</div>';
    }
    if (status == 11) {
        str = '<div id="expression" class="btn wuliu-hook">查看物流</div>' +
            '<div id="refund" class="btn">申请退货</div>' +
            ' <div onclick="confirmOrder('+ order.orderId + ',' + order.orderId +')" id="confirm" class="btn on">确认收货</div>';
    }
    if (status == 3) {
        str = ' <div onclick="topinjia('+order.orderId+')" id="pingjia" class="btn on eva-hook">评价</div>';
    }
    return str;

}
function topinjia(orderId) {
    window.location.href="/evaluate.html?orderId="+orderId;
}